package solr;

import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;

import java.util.Map;

public class SimpleSolrQueryBuilder {
    private final SolrQuery solrQuery;

    public enum Field {
        ALL("*"),
        ID("id"),
        NEGATING_ID("-id"),
        TITLE("title"),
        NEGATING_TITLE("-title"),
        CONTENT("content"),
        NEGATING_CONTENT("-content"),
        CAT_ID("catid"),
        NEGATING_CAT_ID("-catid"),
        RAW_ID("rawid"),
        NEGATING_RAW_ID("-rawid"),
        UPDATE_TIME("updatetime"),
        NEGATING_UPDATE_TIME("-updatetime"),
        AREA("province"),
        NEGATING_AREA("-province"),
        TYPE_FIRST("lba"),
        NEGATING_TYPE_FIRST("-lba"),
        TYPE_SECOND("lbb"),
        NEGATING_TYPE_SECOND("-lbb"),
        STAGE("jzjd"),
        NEGATING_STAGE("-jzjd");

        private String f;

        Field(String f) {
            this.f = f;
        }

        public String getF() {
            return f;
        }

        public void setF(String f) {
            this.f = f;
        }
    }

    public SimpleSolrQueryBuilder(){
        this.solrQuery = new SolrQuery();
    }

    private String ct(Map<Field, Object[]> params) {
        StringBuilder v = new StringBuilder();
        for (Map.Entry<Field, Object[]> entry : params.entrySet()) {
            String field = entry.getKey().getF();
            Object[] values = entry.getValue();

            for (Object value : values) {
                v
                        .append(field)
                        .append(":")
                        .append((value instanceof String && !((String) value).contains("[")) ? "\"" : "")
                        .append(value)
                        .append((value instanceof String && !((String) value).contains("[")) ? "\"" : "")
                        .append(" OR ");
            }
        }

        String v2 = v.toString();

        if (v.lastIndexOf(" OR ") != -1) {
            v2 = v.substring(0, v.lastIndexOf(" OR "));
        }

        if (v2.contains("OR")) {
            v2 = "(" + v2 + ")";
        }
        return v2;
    }

    private void rq(String v1) {
        String v2 = this.solrQuery.getQuery();
        if (StringUtils.isNotEmpty(v2)) {
            solrQuery.set("q", v2 + " AND " + v1);
        }else {
            solrQuery.set("q", v1);
        }
    }

    public SimpleSolrQueryBuilder q(Map<Field, Object[]> params) {
        String v1 = ct(params);
        rq(v1);
        return this;
    }

    public SimpleSolrQueryBuilder q(Field field, Object value) {
        String v1 = value instanceof String ? field.getF() + ":" + "\"" + value + "\"" : field.getF() + ":" + value;
        rq(v1);
        return this;
    }

    public SimpleSolrQueryBuilder fq(Map<Field, Object[]> params) {
        String v1 = ct(params);
        String v2 = v1.replaceAll("\\(|\\)", "");
        this.solrQuery.addFilterQuery(v2);
        return this;
    }

//    public ProjectSubscriptionListSolrRequestQueryBuilder fq(Map<Field, Object>... params) {
//        StringBuilder v1 = new StringBuilder();
//        for (int i = 0; i < params.length; i++) {
//            String v2 = ct(params[i]);
//            v1.
//                    append(" AND ").
//                    append(v2);
//        }
//        String v = v1.toString().replaceAll("^ AND ", "");
//        this.solrQuery.addFilterQuery(v);
//        return this;
//    }

    public SimpleSolrQueryBuilder fl(String fl) {
        this.solrQuery.set("fl", fl);
        return this;
    }

    public SimpleSolrQueryBuilder sort(String sort) {
        this.solrQuery.set("sort", sort);
        return this;
    }

    public SimpleSolrQueryBuilder page(Integer page, Integer pageSize) {
        this.solrQuery.set("start", (page - 1) * pageSize);
        this.solrQuery.set("rows", pageSize);
        return this;
    }

    public SimpleSolrQueryBuilder wt(String wt) {
        this.solrQuery.set("wt", wt);
        return this;
    }

    public SolrQuery build() {
        return solrQuery;
    }
}
